﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using 书易.Model;

namespace 书易.DAL
{
    /// <summary>
    /// 数据访问购物车类
    /// </summary>
    public class TradeDal
    {
        public static List<Trade> SelectYHSP(int mid)
        {
            string sql = "select t.*,b.*,t.BCount 数量,t.BCount*b.BPrice 总价格 from Book b left join Trade t on b.BID=t.BID where t.MID=@mid";
            List<SqlParameter> list = new List<SqlParameter>() { 
                new SqlParameter("@mid",mid)
            };
            List<Trade> li = new List<Trade>();
            using (SqlDataReader dar = DbHelper.ExecuteReader(sql, list.ToArray()))
            {
                while(dar.Read())
                {
                    Trade t = new Trade();
                    t.TID = Convert.ToInt32(dar["TID"]);
                    if (dar["BID"] != DBNull.Value)
                        t.BID = Convert.ToInt32(dar["BID"]);
                    if (dar["MID"] != DBNull.Value)
                        t.MID = Convert.ToInt32(dar["MID"]);
                    if (dar["BCount"] != DBNull.Value)
                        t.BCount = Convert.ToInt32(dar["数量"]);


                    if (dar["总价格"] != DBNull.Value)
                        t.sumJiaG = Convert.ToDouble(dar["总价格"]);


                    if (dar["BSID"] != DBNull.Value)
                        t.BSID = Convert.ToInt32(dar["BSID"]);
                    t.BName = Convert.ToString(dar["BName"]);
                    t.BAuthor = Convert.ToString(dar["BAuthor"]);
                    t.BISBN = Convert.ToString(dar["BISBN"]);
                    t.BTOC = Convert.ToString(dar["BTOC"]);
                    t.BComment = Convert.ToString(dar["BComment"]);
                    t.BPic = Convert.ToString(dar["BPic"]);
                    if (dar["BPrice"] != DBNull.Value)
                        t.BPrice = Convert.ToDouble(dar["BPrice"]);
                    if (dar["BCount"] != DBNull.Value)
                        t.BCount = Convert.ToInt32(dar["BCount"]);
                    if (dar["BDate"] != DBNull.Value)
                        t.BDate = Convert.ToDateTime(dar["BDate"]);
                    if (dar["BSaleCount"] != DBNull.Value)
                        t.BSaleCount = Convert.ToInt32(dar["BSaleCount"]);
                    if (dar["BStatus"] != DBNull.Value)
                        t.BStatus = Convert.ToBoolean(dar["BStatus"]);

                    li.Add(t);
                }
            }
            return li;
        }
        public static int add(Trade t)
        {
            return DbHelper.ExecuteNonQuery("insert into Trade values(@BID,@MID,@BCount)", new SqlParameter[]{
                new SqlParameter("@BID",t.BID),
                new SqlParameter("@BCount",t.BCount),
                new SqlParameter("@MID",t.MID),
            });
        }

        public static int update(Trade t)
        {
            return DbHelper.ExecuteNonQuery("update Trade set BCount=BCount+@BCount where MID=@MID and BID=@BID", new SqlParameter[] { 
                new SqlParameter("@BID",t.BID),
                new SqlParameter("@MID",t.MID),
                new SqlParameter("@BCount",t.BCount)
            });
        }
        public static object SelectBYSP(int bid,int Mid) {
            object o = DbHelper.ExecuteScalar("select * from Trade where BID=@bid and Mid=@mid", new SqlParameter[]{
                new SqlParameter("@bid",bid),
                new SqlParameter("@mid",Mid),
            });
            return o;
        }
        public static int AddOrUpdate(Trade t)
        {
            string te = Convert.ToString(SelectBYSP(Convert.ToInt32(t.BID),Convert.ToInt32(t.MID)));
                if (te=="")
                {
                    return add(t);
                }
                else
                {
                    return update(t);
                }
        }
        public static int del(int tid) {
            return DbHelper.ExecuteNonQuery("delete Trade where TID=@TID",new SqlParameter[]{
                new SqlParameter("@TID",tid),
            });
        }
        public static void updateCount(int TID,int Count) {
            DbHelper.ExecuteNonQuery(string.Format("update Trade set BCount={0} where TID={1}",Count,TID));
        }
    }
}

















